package cn.itcast.web.servlet;

import cn.itcast.domain.Change;
import cn.itcast.service.ChangeService;
import cn.itcast.service.FileService;
import cn.itcast.service.impl.ChangeServiceImpl;
import cn.itcast.service.impl.FileServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/updateChangeInfobServlet")
public class UpdateChangeInfobServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1设置编码
        request.setCharacterEncoding("utf-8");
        String uid = request.getParameter("uid");
        String uname = request.getParameter("uname");
        Map<String, String[]> map = request.getParameterMap();
        Change change =new Change();
        try {
            BeanUtils.populate(change,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        int id = change.getId();
        int yid = change.getYid();
        String infoa = change.getInfo_a();
        String infob = change.getInfo_b();
        //调用service方法
        ChangeService service=new ChangeServiceImpl();
        service.updateChangeInfob(change);
        FileService service1=new FileServiceImpl();
        service1.eva(uid,id,yid,infoa,infob,uname);
       request.getRequestDispatcher("/changeServlet").forward(request,response);
//        response.sendRedirect(request.getContextPath()+"/Changelist.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
